package com.itzhigui.config;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 支付宝配置类
 *
 * @Author: 张兴宇
 * @DateTime: 2023/11/16 10:24
 **/

@Component
public class AlipayConfig implements ApplicationRunner {
//应用id

    @Value("${alipay.appid}")
    private String appId;
    //私钥
    @Value("${alipay.privateKey}")
    private String privateKey;
    //公钥
    @Value("${alipay.publicKey}")
    private String publicKey;
    //支付宝网关
    @Value("${alipay.gateway}")
    private String gateway;
    @Value("${alipay.returnUrl}")
    private String returnUrl;
    //支付成功后的接口回调地址，不是回调的友好页面，不要弄混了
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;

    /**
     * 项目初始化事件
     *
     * @param args incoming application arguments
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //初始化支付宝SDK
        Factory.setOptions(getOptions());
        System.out.println("**********支付宝SDK初始化完成**********");
    }

    private Config getOptions() {
        Config config = new Config();
        config.protocol = "https";  // 设置协议
        config.gatewayHost = this.gateway; // 设置网关
        config.signType = "RSA2"; //设置签名算法
        config.appId = this.appId; //设置应用id
        config.merchantPrivateKey = this.privateKey;  //设置私钥
        config.alipayPublicKey = this.publicKey; //设置支付公钥
        config.notifyUrl = notifyUrl; //可设置异步通知接收服务地址（可选） 走公网
        return config;
    }

    public String getReturnUrl() {
        return returnUrl;
    }
}
